const md5 = require('md5')
const util = require('util')

module.exports = {


  friendlyName: 'Logout',


  description: 'Logout user.',


  inputs: {

  },


  exits: {

  },


  fn: async function (inputs) {
    try{
      const token = this.req.cookies.token
      
      if(!token){ //Token已经过期，等同于已经退出
        return this.res.view('pages/login')
      }
      
      const mtoken = md5(token)
      
      await sails.getDatastore('redis').leaseConnection(async (db)=>{
        await (util.promisify(db.del).bind(db))(mtoken + ':secret');
      })
  
      return this.res.view('pages/login')
    }catch(error){
      sails.log.error(error)
      return this.res.view('pages/exception')
    }
  }
};
